@tool
class_name Skill_Action_Timer
extends SkillAction


#region 属性
@export_range(0.001,4096) var wait_time:float = 1
var left_time:float = 0
var timer:CustomTimer
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
## 初始化
func init(action_idx:int = -1,init_data:Dictionary = {},handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
			left_time = wait_time
	super.init(action_idx,init_data,self_handle_func)
## 释放
func release(args:Dictionary = {},handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
			var agent = get_agent()
			timer = GameCustomTimerSystem.create_timer(left_time)
			connect_obj_dict[timer.timer_finished] = ConnectObj.new(timer.timer_finished,[_on_timer_finished])
			timer.start_timer()
	super.release(args,self_handle_func)
## 打断
func interrupt(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
			if timer:
				timer.pause_timer()
	super.interrupt(self_handle_func)
## 完成
func complete(release_result:GameEnum.SkillReleaseResult = GameEnum.SkillReleaseResult.Success,handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
			if timer:
				GameCustomTimerSystem.remove_timer(timer.id)
				timer.pause_timer()
	super.complete(release_result,self_handle_func)
## 冷却
func cool(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
	super.cool(self_handle_func)
## 冷却完成
func cool_complete(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
			if timer:
				timer.reset_timer()
	super.cool_complete(self_handle_func)
## 重置冷却
func reset_cool(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
			if timer:
				timer.reset_timer()
	super.reset_cool(self_handle_func)
## 销毁
func destroy(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
			if timer:
				GameCustomTimerSystem.remove_timer(timer.id)
				if connect_obj_dict.has(timer.timer_finished):
					connect_obj_dict.get(timer.timer_finished).disconnect_all()
					connect_obj_dict.erase(timer.timer_finished)
				timer = null
	super.destroy(self_handle_func)
## 判断该技能能否释放
func check_can_release() -> GameEnum.SkillReleaseStatus:
	var res = super.check_can_release()
	if res != GameEnum.SkillReleaseStatus.Success:
		return res
	var agent = get_agent()
	if not agent:
		return GameEnum.SkillReleaseStatus.Failure
	return GameEnum.SkillReleaseStatus.Success
#endregion
#region 公共方法
#endregion
#region 私有方法
func _on_timer_finished(timer_result:CustomTimer.TimerResult):
	if timer_result == CustomTimer.TimerResult.Cancel:
		complete(GameEnum.SkillReleaseResult.Failure)
	elif timer_result == CustomTimer.TimerResult.Completed:
		complete(GameEnum.SkillReleaseResult.Success)
#endregion
#region 生命周期函数
#endregion
